<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="isshow"
      width="600px"
      :before-close="resetForm"
    >
      <div class="dialogContent">
        <div class="msgItem">
          <el-form :model="form" :rules="rules" ref="form">
            <el-form-item label="学校名称" label-width="100px" required prop="schoolId">
              <el-select v-model="form.schoolId" placeholder="请选择学校名称" :disabled="type!=='add'">
                <el-option :label="item.schoolName" :value="item.schoolId" v-for="(item) in schoolList"
                           :key="item.schoolId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="起止日期" label-width="100px" required>
              <el-col :span="11">
                <el-form-item prop="startDate">
                  <el-date-picker type="date" placeholder="选择日期" :disabled="type!=='add'" v-model="form.startDate"
                                  style="width: 100%;"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col class="line" :span="1">至</el-col>
              <el-col :span="11">
                <el-form-item prop="deadlineDate">
                  <el-date-picker type="date" placeholder="选择日期" v-model="form.deadlineDate" style="width: 100%;"
                                  :picker-options="pickerOptionsEnd"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item label="资源库总量" label-width="100px" required prop="totalStorageSize">
              <el-input-number v-model="form.totalStorageSize" :min="0"></el-input-number>
              GB
            </el-form-item>
          </el-form>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="resetForm">取 消</el-button>
    <el-button type="primary" @click="submitForm">保 存</el-button>
  </span>
    </el-dialog>
  </div>
</template>
<script>
import { addStorageConfig, editStorageConfig, getSchoolconfigInfo, getSchoolLisst } from '@/api/library/librarySet'

export default {
  name: 'updateDialog',
  data() {
    return {
      title: '添加学校',
      type: 'add',
      isshow: false,
      form: {
        schoolId: void 0,
        startDate: '',
        deadlineDate: '',
        totalStorageSize: 200
      },
      vuecutshow: false,
      schoolList: [],
      rules: {
        schoolId: [
          { required: true, message: '请选择学校名称', trigger: 'change' }
        ],
        startDate: [
          { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        ],
        deadlineDate: [
          { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
        ],
        totalStorageSize: [
          { required: true, message: '请设置资源库总量', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    pickerOptionsEnd() {
      const that = this
      return {
        disabledDate(time) {
          if (!that.form.startDate) {
            return
          }
          const startDate = new Date(that.form.startDate)
          const nowDate = new Date()
          startDate.setDate(startDate.getDate())
          nowDate.setDate(nowDate.getDate())
          if (startDate.getTime() < nowDate.getTime()) {
            return time.getTime() < nowDate.getTime()
          } else {
            return time.getTime() < startDate.getTime()
          }

        }
      }
    }
  },
  methods: {
    show(type, form) {
      this.type = type
      if (type === 'add') {
        this.title = '添加学校'
      } else {
        this.title = '编辑权限'
      }
      this.isshow = true
      this.getschoolList()
      if (form) {
        this.getschoolInfo(form.schoolId)
      }

    },

    handleClose() {
      this.isshow = false
    },
    editCropper() {
      this.vuecutshow = true
    },
    coverResult(e) {
      this.form.schoolLogo = e
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.type === 'add') {
            this.addSchool()
          } else {
            this.upDateSchool()
          }
          this.isshow = false
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.form = {
        schoolId: '',
        startDate: '',
        deadlineDate: '',
        totalStorageSize: 200
      }
      this.isshow = false
      this.$refs['form'].resetFields()
    },
    // 添加
    async addSchool() {
      let res = await addStorageConfig(this.form)
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: `添加成功`
        })
        this.closeDialog()
        this.$parent.getList()
      }
    },
    // 修改
    async upDateSchool() {
      let res = await editStorageConfig(this.form)
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: `修改成功`
        })
        this.closeDialog()
        this.$parent.getList()
      }
    },
    // 获取详情
    async getschoolInfo(id) {
      let res = await getSchoolconfigInfo({ schoolId: id })
      if (res.code == 200) {
        this.form = res.data
        this.form.startDate = new Date(this.form.startDate)
        this.form.deadlineDate = new Date(this.form.deadlineDate)
      }
    },
    // 获取学校列表
    async getschoolList() {
      let res = await getSchoolLisst({ dataType: 0 })
      if (res.code == 200) {
        this.schoolList = res.data
        if (this.type == 'add') {
          this.schoolList = this.schoolList.filter(e => e.dataType != 2)
        }
      }
    },
    // 关闭弹窗
    closeDialog() {
      this.form = {
        schoolId: '',
        startDate: '',
        deadlineDate: '',
        totalStorageSize: 200
      }
      this.isshow = false
    }
  }
}

</script>

<style scoped lang="scss">
.dialogContent {
  .msgItem {
    .line {
      text-align: center;
    }
  }
}

</style>
